//
//  WBMainController.swift
//  weiboNine
//
//  Created by 陈逸麒 on 2017/4/2.
//  Copyright © 2017年 chenyq. All rights reserved.
//

import UIKit

class WBMainController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.white

        setupUI()
        
    }

}

//MARK: - 点击事件
extension WBMainController{
    
    @objc fileprivate func compose(button: UIButton) {
        print("发布微博")
    }
    
}

//MARK: - 设置UI
extension WBMainController {
    /// 设置UI
    fileprivate func setupUI () {
        self.view.backgroundColor = UIColor.white
        //self.tabBar.tintColor = UIColor.orange
        
        //添加所有的子控件器
        addChildControllers()
        
        //添加tabbar底部的发布按钮
        setCompseButton()
        
        //设置tabbar的阴影线条
        setupShadowImage()
    }
    
    /// 设置tabbar的阴影线条, 下面的两个属性必须都要设
    func setupShadowImage () {
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
        tabBar.shadowImage = UIImage.pureImage(color: UIColor(white: 0.9, alpha: 0.9))
    }
    
    /// 添加发布按钮
    fileprivate func setCompseButton () {
        let button = UIButton(title: nil, image: "tabbar_compose_icon_add", bgImage: "tabbar_compose_button", target: self, action: #selector(compose(button:)))
        
        //设置frame
        //indetBy, 在前面的frame为中心点, 向内或向外扩展
        let width = tabBar.frame.width/5
        button.frame = tabBar.bounds.insetBy(dx: width*2-1, dy: -6)
        
        //将按钮添加到tabbar上
        tabBar.addSubview(button)
    }
    
    /// 添加所有的子控制器
    fileprivate func addChildControllers () {
        //获取url
        if let url = Bundle.main.url(forResource: "main.json", withExtension: nil),
            //使用url, 获取data
            let jsonData = try? Data(contentsOf: url),
            //使用data,对jsonData做反序列化操作, 返回的是一个可选的Any类型
            let object = try? JSONSerialization.jsonObject(with: jsonData, options: []),
            //将Any类型转成 字典数组
            let dicArry = object as? [[String: Any]]  {
            
            //创建空数组
            var controllers: [UINavigationController] = []
            
            //遍历参数字典
            for dic in dicArry {
                //创建子控制器
                let nav = generateChildController(dict: dic)
                controllers.append(nav!)
            }
            
            //设置所有的子控制器
            self.viewControllers = controllers
        }
    }
    
    /// 创建单个字控制器
    fileprivate func generateChildController(dict: [String: Any]) -> UINavigationController? {
        //通过字符串, 创建类, 并使用类来创建控制器
        if let clsName = dict["clsName"] {
            //类名在swift中, 必须加一个项目名的前缀
            let className = "weiboNine" + "." + "\(clsName as! String)"
            
            //使用字符串来创建类, 注意必须转成 Type类型
            if let cls = NSClassFromString(className) as? UIViewController.Type {
                //使用类, 来创建一个子控制器对象
                let controller = cls.init()
                
                //给子控制器设置title
                let title = dict["title"] as? String
                controller.title = title
                controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.gray], for: .normal)
                controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .selected)
                
                
                //给子控制器的tabbarItem设图片
                if let imageName = dict["imageName"] {
                    let image = UIImage(named: "tabbar_\(imageName)")?.withRenderingMode(.alwaysOriginal)
                    let selectedImage = UIImage(named: "tabbar_\(imageName)_selected")?.withRenderingMode(.alwaysOriginal)
                    
                    controller.tabBarItem.image = image
                    controller.tabBarItem.selectedImage = selectedImage
                }
                
                //使用子控制器创建navigationController
                let nav = UINavigationController(rootViewController: controller)
                
                return nav
            }
        }
        
        return nil
    }
}




